package com.llkj.qingnianguoxue.mine.mycourse;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StudentCourseActivity extends BaseActivity {

    @BindView(R.id.fl_book)
    FrameLayout flBook;//已预约
    @BindView(R.id.fl_complete)
    FrameLayout flComplete;//已完结
    private MyCourseFragment mBookFragment,mCompleteFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_course);
        setTitle("我的课程", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1,-1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);

        setSelection(0);
    }

    @OnClick({R.id.fl_book, R.id.fl_complete})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fl_book://已预约
                setSelection(0);
                break;
            case R.id.fl_complete://已完结
                setSelection(1);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flBook);
                if (mBookFragment == null) {
                    mBookFragment = MyCourseFragment.newInstance(0);
                    fragmentTransaction.add(R.id.content_layout, mBookFragment, "tab1");
                } else {
                    fragmentTransaction.show(mBookFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flComplete);
                if (mCompleteFragment == null) {
                    mCompleteFragment = MyCourseFragment.newInstance(1);
                    fragmentTransaction.add(R.id.content_layout, mCompleteFragment, "tab2");
                } else {
                    fragmentTransaction.show(mCompleteFragment);
                }

                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flBook.setSelected(false);
        flComplete.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (mBookFragment != null) {
            transaction.hide(mBookFragment);
        }
        if (mCompleteFragment != null) {
            transaction.hide(mCompleteFragment);
        }
    }
}
